package controllers.listeners.feature;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import models.FeaturesModel;
import models.LexiconFeaturesModel;
import views.ViewFeatures;

/* Saves the data into the xml file */
/* actionPerformed when you click the "Save" button */
public class SaveFeaturesActionListener implements ActionListener {
	
	private ViewFeatures view;
	
	public SaveFeaturesActionListener() {
		view = null;
	}
	
	public void setView(ViewFeatures view) {
		this.view = view;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(view.getSelectedFeatureType().equals("Semantic Rep")) {
			try {
				FeaturesModel.write();
				JOptionPane.showMessageDialog(null, "Save successful!", "Success!", JOptionPane.PLAIN_MESSAGE);
			}
			catch (Exception ex) {
				JOptionPane.showMessageDialog(null,	"Save failed!", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		else if(view.getSelectedFeatureType().equals("Lexicon")) {
			try {
				LexiconFeaturesModel.write();
				JOptionPane.showMessageDialog(null, "Save successful!", "Success!", JOptionPane.PLAIN_MESSAGE);
			}
			catch (Exception ex) {
				JOptionPane.showMessageDialog(null,	"Save failed!", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
